﻿using GylCSharpUtils.Common.Abstract;
using GylCSharpUtils.GSerializer.Abstract;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace GylCSharpUtils.Common.Base.SpecialConverter {
    public abstract class ASpecialConverter : AConverter {

        /// <summary>
        /// 该专有序列化器处理的类型
        /// </summary>
        public Type SpecialType { get; private set; }

        
        public ASpecialConverter() {
            SpecialType =  ConfigType();
        }

        /// <summary>
        /// 用于配置该类处理类型
        /// </summary>
        /// <returns></returns>
        public abstract Type ConfigType();

        public override byte[] GetBytes(object obj) {
            if(obj == null) return null;
            if(obj.GetType() == SpecialType) {
                return ToByteArray(obj);
            }
            throw new Exception($"转换类型不匹配,{obj.GetType().FullName} --> {ConfigType().FullName}");
        }

        public override object GetObject(byte[] data) {
            if(data == null) return null;
            return ToObject(data);
        }
        


        /// <summary>
        /// 将该类型的数据转为字节数组，不需要判空
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="value"></param>
        /// <returns></returns>
        public abstract byte[] ToByteArray(object value);

        public abstract object ToObject(byte[] data);

        
    }


    
}
